package com.codewsq;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Rectangle extends JComponent {
    public Rectangle() {
        setDoubleBuffered(true);
    }

    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;
        Arc2D arc;
        float[] solid = { 12.0f, 0.0f };
        // 消除锯齿边缘
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        Dimension size = getSize();
        g2.setColor(Color.white);
        g2.fill(new Rectangle2D.Double(0, 0, size.width, size.height));
        // 控制线段的颜色类型
        BasicStroke bs = new BasicStroke(2.0f, BasicStroke.CAP_SQUARE,
                BasicStroke.JOIN_MITER, 1.0f, solid, 0.0f);
        g2.setStroke(bs);
        arc=new Arc2D.Double(20,40,100,150,0,60,Arc2D.PIE);
        g2.setColor(Color.orange); g2.fill(arc); g2.setColor(Color.red);
        g2.draw(arc);



         arc=new Arc2D.Double(10,200,100,100,90,180,Arc2D.CHORD);
         g2.setColor(Color.orange); g2.draw(arc);


         arc=new Arc2D.Double(0,0,200,200,0,360,Arc2D.OPEN);
         g2.setColor(Color.orange);
         g2.fill(arc);
         g2.setColor(Color.red);
         g2.draw(arc);



         arc=new Arc2D.Double(220,220,100,100,-30,-300,Arc2D.PIE);
         g2.setColor(Color.orange); g2.fill(arc); g2.setColor(Color.red);
         g2.draw(arc);


    }

    public static void main(String args[]) {
        MyWindowListener l = new MyWindowListener();
        Rectangle c = new Rectangle();

        JFrame fr = new JFrame("Test...");
        fr.addWindowListener(l);
        fr.getContentPane().add(c, BorderLayout.CENTER);
        fr.pack();
        fr.setSize(400, 400);
        fr.setLocation(400, 400);
        // fr.show();
        fr.setVisible(true);

    }

}

class MyWindowListener extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}
